package com.example.chuntiao.cophook.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.beans.Person;
import com.example.chuntiao.cophook.mytools.MyApp;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;


public class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "PersonAdapter";
    static final int TYPE_HEADER = 0;
    static final int TYPE_GAP = 1;
    static final int TYPE_NORMAL = 2;

    private ArrayList<Person> mPersonList;
    private ArrayList<Person> copyOfmPersonList;
    private boolean mHasHead;
    private Context mContext;
    private PersonAdapter.OnclickListener mListener;
    private String oid;
    private String intro;
    private int orgImgVer;

    public void setOnClickListener(PersonAdapter.OnclickListener listener) {
        this.mListener = listener;
    }

    public interface OnclickListener {
        void OnNormalBodyLongClick(View v, int position);

        void OnNormalBodyClick(View v, int position);

        void OnNormalPhoneClick(View v, int position);
    }

    public void setmPersonList(ArrayList<Person> mPersonList) {
        this.mPersonList = mPersonList;
        copyOfmPersonList = (ArrayList<Person>) mPersonList.clone();
    }

    public ArrayList<Person> getmPersonList() {
        return mPersonList;
    }

    public PersonAdapter(boolean hasHead, String oid, String intro, int orgImgVer) {
        mPersonList = new ArrayList<>();
        mHasHead = hasHead;
        this.oid = oid;
        this.intro = intro;
        this.orgImgVer = orgImgVer;
    }

    public PersonAdapter(ArrayList<Person> PersonList, boolean hasHead) {
        mPersonList = PersonList;
        copyOfmPersonList = (ArrayList<Person>) PersonList.clone();
        mHasHead = hasHead;
    }


    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        mContext = parent.getContext();
        switch (viewType) {
            case TYPE_NORMAL:
                View view = LayoutInflater.from(mContext).inflate(R.layout.person_item_normal, parent, false);
                holder = new NormalHolder(view);
                break;

            case TYPE_GAP:
                View viewG = LayoutInflater.from(mContext).inflate(R.layout.person_item_gap, parent, false);
                holder = new GapHolder(viewG);
                break;

            case TYPE_HEADER:
                View viewHeader = LayoutInflater.from(mContext).inflate(R.layout.person_item_header, parent, false);
                holder = new HeaderHolder(viewHeader);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        Person person = null;
        if (mHasHead) {
            if (position != 0)
                person = mPersonList.get(position - 1);
        } else person = mPersonList.get(position);

        switch (holder.getItemViewType()) {

            case TYPE_HEADER:
                HeaderHolder tHeadHolger = (HeaderHolder) holder;
                if (orgImgVer == -1) {
                    tHeadHolger.headLayout.setVisibility(View.GONE);
                    tHeadHolger.headerPhoto.setVisibility(View.GONE);
                } else {
                    tHeadHolger.textIntro.setText(R.string._2012_2015);
                    String a = tHeadHolger.textIntro.getText().toString();
                    tHeadHolger.textIntro.setText(intro);
                    Glide.with(mContext).load(MyApp.HOST_URL + "image/o_" + oid)
                            .transition(new DrawableTransitionOptions().crossFade())
                            .apply(RequestOptions.signatureOf(new ObjectKey(orgImgVer)))
                            .into(tHeadHolger.headerPhoto);
                }
                break;

            case TYPE_GAP:
                GapHolder tGHolder = (GapHolder) holder;
                tGHolder.textGap.setText(person.getPyOfHomeName());
                break;

            case TYPE_NORMAL:
                NormalHolder tNormalHolder = (NormalHolder) holder;
                tNormalHolder.personImage.setImageResource(R.drawable.ic_people_light);
                if (person.getImgVer() != -1) {
                    Glide.with(mContext).load(MyApp.HOST_URL + "image/" + "u_" + person.getUid())
                            .transition(new DrawableTransitionOptions().crossFade())
                            .apply(RequestOptions.signatureOf(new ObjectKey(person.getImgVer())))
                            .into(tNormalHolder.personImage);
                }
                tNormalHolder.phone1.setText(person.getPhone1());
                tNormalHolder.phone2.setText(person.getPhone2());
                tNormalHolder.personName.setText(person.getName());
                tNormalHolder.personView.setTag(R.id.uid, person.getUid());
                tNormalHolder.personView.setTag(R.id.position, position);//TODO　注意检查正确性

                tNormalHolder.personView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mListener.OnNormalBodyLongClick(v, position);
                        return true;
                    }
                });
                tNormalHolder.personView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.OnNormalBodyClick(v, position);
                    }
                });
                tNormalHolder.toCall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.OnNormalPhoneClick(v, position);
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (mHasHead)
            return mPersonList.size() + 1;
        return mPersonList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && mHasHead)
            return TYPE_HEADER;
        Person person;
        if (mHasHead)
            person = mPersonList.get(position - 1);
        else
            person = mPersonList.get(position);

        if (person.getUid() == null || "".equals(person.getUid()))
            return TYPE_GAP;
        else
            return TYPE_NORMAL;
    }

    public void doSearch(String tag) {
        if ("".equals(tag)) {
            cancelSearch();
            return;
        }

        ArrayList<Person> toHideList = new ArrayList<>();
        for (Person p : mPersonList) {
            if (p.getName() == null ||
                    (!p.getName().contains(tag)
                            && !p.getPyOfHomeName().contains(tag.toUpperCase())
                            && !p.getPhone1().contains(tag)
                            && !p.getPhone2().contains(tag)
                    )
                    )

                toHideList.add(p);
        }

        for (Person p : toHideList) {
            mPersonList.remove(p);
        }
        this.notifyDataSetChanged();

    }

    public void cancelSearch() {
        mPersonList.clear();
        mPersonList = (ArrayList<Person>) copyOfmPersonList.clone();
        notifyDataSetChanged();
    }
}

class MyBaseHolder extends RecyclerView.ViewHolder {

    int mType;

    public MyBaseHolder(View view) {
        super(view);
    }

}

/*创建三种类型的ViewHolder*/
class NormalHolder extends MyBaseHolder {
    View personView;
    CircleImageView personImage;
    TextView personName;
    TextView phone1;
    TextView phone2;
    View toCall;


    private static final String TAG = "NormalHolder";


    public NormalHolder(View view) {
        super(view);
        mType = PersonAdapter.TYPE_NORMAL;
        personView = view;
        personImage = (CircleImageView) view.findViewById(R.id.img_person);
        personName = (TextView) view.findViewById(R.id.text_name);
        phone1 = (TextView) view.findViewById(R.id.text_uid);
        phone2 = (TextView) view.findViewById(R.id.texe_phone2);
        toCall = view.findViewById(R.id.to_call);
    }
}

class HeaderHolder extends MyBaseHolder {

    View orgView;
    ImageView headerPhoto;
    RelativeLayout headLayout;
    TextView textIntro;

    public HeaderHolder(View view) {
        super(view);
        mType = PersonAdapter.TYPE_HEADER;
        orgView = view;
        headerPhoto = (ImageView) view.findViewById(R.id.img_org_photo);
        textIntro = (TextView) view.findViewById(R.id.txt_org_intro);
        headLayout = (RelativeLayout) view.findViewById(R.id.relative_layout_head);

    }
}

class GapHolder extends MyBaseHolder {

    View personView;
    TextView textGap;

    public GapHolder(View view) {
        super(view);
        mType = PersonAdapter.TYPE_GAP;
        personView = view;
        textGap = (TextView) view.findViewById(R.id.text_gap);
    }
}
